//
//  LCTabController.m
//  04 -自定制tabBar
//
//  Created by 千锋 on 16/5/7.
//  Copyright (c) 2016年 千锋. All rights reserved.
//

#import "LCTabController.h"
#import "LCTabBar.h"
#import "FunctionViewController.h"
#import "myViewController.h"
@interface LCTabController()<LCTableBarDelegate>

//自定制的tabBar
@property(nonatomic,strong)LCTabBar *customTabBar;

@end

@implementation LCTabController
#pragma mark - 切换视图控制器
- (void)changeCurrentController{
    self.selectedIndex = self.customTabBar.selectedIndex;
}
#pragma mark -自定制的tabBar
- (LCTabBar *)customTabBar{
    if (!_customTabBar) {
        
        //创建自定制的tabBar的对象
        _customTabBar = [[LCTabBar alloc]initWithFrame:self.tabBar.bounds];
        _customTabBar.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
        //将自定制的tabBar显示在系统的上面
        //设置代理
        _customTabBar.delegate = self;
        UIImageView *centerImage =[[UIImageView alloc ]initWithFrame:CGRectMake(0, 0, 40, 35)];
        centerImage.image = [[UIImage imageNamed:@"tab-bar-chat-icon-selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
        _customTabBar.centerView = centerImage;
        //增加手势到中间视图上去
        centerImage.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onCenterImage)];
        [centerImage addGestureRecognizer:tap];
        [self.tabBar addSubview:_customTabBar];
    }
    return _customTabBar;
}
#pragma mark - 手势的实现
- (void)onCenterImage{
   
    
    AVUser *user = [AVUser currentUser];
    if (user) {
        FunctionViewController *func = [FunctionViewController new];
        UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:func];
        [nav setNavigationBarHidden:YES];
        
        [self presentViewController:nav animated:YES completion:nil];
    }else{
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"亲，抱歉" message:@"先登录嘛，科科~~" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
        [alert show];
    }
  
}
//在这个方法中移除系统创建的每个视图控制器对应的按钮
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    
  
    //移除tabBar上系统创建的按钮
    for (UIView *view in self.tabBar.subviews) {
        if (![view isKindOfClass:[LCTabBar class]]) {
            [view removeFromSuperview];
        }
    }
}

- (void)addController:(UINavigationController *)controller WithTitle:(NSString *)title imageName:(NSString *)name selectedImageName:(NSString *)selectedImageName{
   //1.设置Controller的tableBarItem的属性

    UIViewController *viewController = (UIViewController *)[controller.viewControllers objectAtIndex:0];
    
    viewController.title = title ;
    controller.tabBarItem.title = title;
    controller.tabBarItem.image = [UIImage imageNamed:name];
    controller.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImageName]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal ];
     [controller.navigationBar setBarTintColor:[UIColor colorWithRed:86/255.0f green:202/255.0f blue:255/255.0f alpha:0.5]];
    controller.navigationBar.translucent = NO;
    [controller.navigationBar setBarStyle:UIBarStyleBlack];
   
    //2.添加到标签栏控制器中（标签栏并不是addsubView！！！）
    [self addChildViewController:controller];
   self.customTabBar.item = controller.tabBarItem;
    
}


@end
